package com.io;

import java.io.*;

/**
 * 功能描述：将图书订单的信息利用DataInputStream 和DataOutputStream写入文件
 * ，然后从文件中读出，显示在控制台上。图书信息的三组数据如下：
 * {“Java”,”Sql”,”Jsp”};
 * {10,15,22}
 * {105.5,65.5,59.5}
 *
 * @author: Jovi
 * @createTime: 2017-06-09 15:45
 **/
public class DataInputAndOutput {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        DataOutputStream dos = null;
        DataInputStream dis = null;
        float[] b = {105.5f,65.5f,59.5f};
        int[] a = {10,15,22};
        String[] str = {"Java","Jsp","Sql"};
        try {
            fos = new FileOutputStream("t.txt");
            dos = new DataOutputStream(new BufferedOutputStream(fos));
            for (int i = 0; i < 3; i++) {
                dos.writeUTF(str[i]);
                dos.writeInt(a[i]);
                dos.writeFloat(b[i]);
                dos.writeChar('\n');
            }

            dos.flush();

            fis = new FileInputStream("t.txt");
            dis = new DataInputStream(new BufferedInputStream(fis));

            double price;
            int amount;
            String name;
            System.out.println("your order is \nname\tnumber\tprice");
            while (dis.available() != 0) {
                name = dis.readUTF();
                amount = dis.readInt();
                price = dis.readFloat();
                System.out.println(name+"\t"+amount+"\t"+price);
                dis.readLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                dis.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
